dokcer exec
1 概述
dokcer exec
可以不进入容器,直接执行任意命令。
2 语法大纲
$ docker exec --help
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Run a command in a running container
Options:
-d, --detach Detached mode: run command in the background
--detach-keys string Override the key sequence for detaching a container
-e, --env list Set environment variables
--env-file list Read in a file of environment variables
-i, --interactive Keep STDIN open even if not attached
--privileged Give extended privileges to the command
-t, --tty Allocate a pseudo-TTY
-u, --user string Username or UID (format: <name|uid>[:<group|gid>])
-w, --workdir string Working directory inside the container
3 参数简介
参数 | 说明 |
---|---|
-d, --detach | 分离模式:在后台运行命令。 |
--detach-keys string | 指定退出 attach 模式的快捷键序列,默认是 CTRL-p CTRL-q。 |
-e, --env list | 设置环境变量。 |
--env-file list | 从一个文件中读取环境变量。 |
i, --interactive | 打开标准输入接受用户输入命令,默认为 false; |
--privileged | 否给执行命令以高权限,默认为 false; |
-t, --tty | 分配伪终端,默认为 false; |
-u, --user string | 执行命令的用户名或 ID。 |
-w, --workdir string | 指定容器的工作目录。 |
编者注:再生产环境下,尽量使用 docker exec 命令,因为这种操作不影响当前其他应用。 |
4 进入创建的容器中,并启动一个 bash
$ docker exec -it kcp_centos /bin/bash
//屏幕输出:
root@971ff7ed7baf:/#
Note
通过指定 -it
方式可以保持标准输入代开,并且分配一个伪终端。推荐通过 docker exec
命令操作容器。